package com.drama.api.sys.authority.service.impl;

import com.drama.api.sys.authority.dao.SysAuthorityDao;
import com.drama.api.sys.authority.domain.vo.SysAuthorityTreeSearchVO;
import com.drama.api.sys.authority.domain.vo.SysAuthorityTreeVO;
import com.drama.api.sys.authority.service.ISysAuthorityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
@Transactional
@Slf4j
public class SysAuthorityServiceImpl implements ISysAuthorityService {
    @Autowired
    private SysAuthorityDao sysAuthorityDao;

    @Override
    public List<SysAuthorityTreeVO> getTreeList(SysAuthorityTreeSearchVO searchVO) {
        List<SysAuthorityTreeVO> rList = new ArrayList<>();
        List<SysAuthorityTreeVO> list = sysAuthorityDao.getTreeList(searchVO);
        HashMap<String, SysAuthorityTreeVO> map = new HashMap<>();
        for (SysAuthorityTreeVO vo : list) {
            map.put(vo.getId(), vo);
        }
        for (SysAuthorityTreeVO vo : list) {
            if (vo.getParentId() == null) {
                continue;
            }
            if (vo.getParentId().equals("0")) {
                rList.add(vo);
            } else {
                SysAuthorityTreeVO parentVO = map.get(vo.getParentId());
                if (parentVO == null) {
                    rList.add(vo);
                    continue;
                }
                if (parentVO.getChildren() == null) {
                    parentVO.setChildren(new ArrayList<>());
                }
                parentVO.getChildren().add(vo);
            }
        }
        return rList;
    }

    @Override
    public List<String> getAuthorityIds(SysAuthorityTreeSearchVO sysAuthorityTreeSearchVO) {
        return sysAuthorityDao.getAuthorityIds(sysAuthorityTreeSearchVO);
    }

}